<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">

    </avue-crud>

  </basic-container>
</template>

<script>
import { getCyclingPageList } from "./api/cycling";
import { getSystemUserList } from "../systemManage/systemUserManage/api/user";

import { mapGetters } from "vuex";
import website from "@/config/website";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      selectionList: [],
      option: {
        menu: false,
        menuType: 'button',
        height: "auto",
        calcHeight: 30,
        tip: false, // 当前表格已选择多少项 提示语
        searchShow: true,
        searchMenuSpan: 6,
        border: false,

        index: true,
        editBtn: false,
        selection: false,
        dialogClickModal: false,
        addBtn: false,
        delBtn: false,
        column: [
          {
            label: "用户",
            prop: "nickname",
            type: 'select',
            search: true,
            hide: true, //表格是否显示该字段（该字段的列）
            display: false, //弹出的表单是否显
            dicData: [],
          },
          {
            label: "骑行轨迹名称",
            prop: "name",
          },
          {
            label: "开始时间",
            prop: "start_time",
            type: "datetime",
          },
          {
            label: "结束时间",
            prop: "end_time",
            type: "datetime",
          },// 总运动时长
          {
            label: "总运动时长(秒)",
            prop: "duration",
          },
          {
            label: "累计暂停时长(秒)",
            prop: "pause_duration",
          },
          {
            label: "平均速度(km/h)",
            prop: "avg_speed",
          },
          {
            label: "最大速度(km/h)",
            prop: "max_speed",
          },
          {
            label: "消耗千卡",
            prop: "calories",
          },
          {
            label: "骑行轨迹",
            prop: "path",
          },
          {
            label: "状态",
            prop: "status",
            type: 'select',
            search: true,
            dicData: [{
              label: '进行中',
              value: 0
            }, {
              label: '暂停',
              value: 1
            }, {
              label: '结束',
              value: 2
            }],
          },
          {
            label: "最后一次暂停时间",
            prop: "last_paused_time",
          },
        ],
      },
      data: [],
      userInfo: {}
    };
  },

  computed: {
    ...mapGetters(["permission"]),

    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.user_add, true),
        viewBtn: this.vaildData(this.permission.user_view, true),
        delBtn: this.vaildData(this.permission.user_delete, true),
        editBtn: this.vaildData(this.permission.user_edit, true),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.account_id);
      });
      return JSON.stringify(ids);
    },
  },
  created() {
    const user = localStorage.getItem(website.key + "-user");
    if (user) this.userInfo = JSON.parse(user);
    this.getUserData()
  },
  mounted() { },
  methods: {
    // 获取用户
    getUserData() {
      getSystemUserList({
        "page": 1,
        "size": 999999
      }).then((res) => {
        const resData = res.data.list;
        this.option.column[0].dicData = resData.map((r, index) => {
          return {
            label: r.nickname,
            value: r.id
          }
        })
      });
    },
    rowView(row, index) {
      this.$refs.crud.rowView(row, index);
    },
    rowEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    rowDel(row, index) {
      this.$refs.crud.rowDel(row, index);
    },
    rowSave(row, done, loading) {
      return
      let param = {
        uid: this.userInfo.id,
        ...row
      }
      xxx(param).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      return
      let params = {
        "uid": this.userInfo.id,
        ...row
      }
      if (typeof row.province === 'object' && row.province !== null) {
        params.province = row.province.code
      }
      if (typeof row.city === 'object' && row.city !== null) {
        params.city = row.city.code
      }
      if (typeof row.area === 'object' && row.area !== null) {
        params.area = row.area.code
      }
      xxx(params).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      return
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return xxx({ id: row.id });
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },



    beforeOpen(done, type) {

      done();
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(page_cursor) {
      this.page.currentPage = page_cursor;
    },
    sizeChange(page_size) {
      this.page.pageSize = page_size;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let param = {
        ...params,
        page: page.currentPage,
        size: page.pageSize
      }
      getCyclingPageList(param).then((res) => {
        const resData = res.data;
        this.page.total = resData.total;
        this.data = resData.list;
        this.loading = false;
        this.selectionClear();
      });
    },


    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
  },
};
</script>


<style lang="scss" scoped></style>